Expert C++ by Vardan Grigoryan

Expert C++ by Vardan Grigoryan

Author:Vardan Grigoryan
Language: eng
Format: mobi, epub
Tags: COM051210 - COMPUTERS / Programming / Object Oriented, COM051230 - COMPUTERS / Software Development and Engineering / General, COM051070 - COMPUTERS / Programming Languages / C++
Publisher: Packt
Published: 2020-04-10T09:44:25+00:00


Technical requirements

The g++ compiler with the -std=c++2a option is used to compile the examples in this chapter. You can find the source files used in this chapter at https://github.com/PacktPublishing/Expert-CPP .

Understanding concurrency and multithreading

The simplest form of running a program involves its instructions being executed one by one by the CPU (Central Processing Unit). As you already know from previous chapters, a program consists of several sections, one of them containing the instructions of the program. Each instruction is loaded into a CPU register for the CPU to decode and execute it. It doesn't actually matter what programming paradigm you use to produce an application; the result is always the same—the executable file contains machine code.

We mentioned that programming languages such as Java and C# use support environments. However, if you cut down the support environment in the middle (usually, the virtual machine), the final instructions being executed should have a form and format familiar to that particular CPU. It's obvious to programmers that the order of statements run by the CPU is not mixed in any circumstance. For example, we are sure and can continue to be so that the following program will output 4, "hello", and 5, respectively:

int a{4};

std::cout << a << std::endl;

int b{a};

++b;

std::cout << "hello" << std::endl;

b--;

std::cout << (b + 1) << std::endl;

We can guarantee that the value of the a variable will be initialized before we print it to the screen. The same way we can guarantee that the "hello" string will be printed before we decrement the value of b, and that the (b + 1) sum will be calculated before printing the result to the screen. The execution of each instruction might involve reading data from or writing to memory.

As introduced in Chapter 5, Memory Management and Smart Pointers, the memory hierarchy is sophisticated enough to make our understanding of program execution a little bit harder. For example, the int b{a}; line from the previous example assumes that the value of a is loaded from the memory into a register in the CPU, which then will be used to write into the memory location of b. The keyword here is the location because it carries a little bit of special interpretation for us. More specifically, we speak about memory location. Concurrency support depends on the memory model of the language, that is, a set of guarantees for concurrent access to memory. Although the byte is the smallest addressable memory unit, the CPU works with words in data. That said, the word is the smallest unit the CPU reads from or writes to memory. For example, we consider the following two declarations separate variables:

char one;

char two;

If those variables are allocated in the same word (considering the word size as bigger than the size of a char), reading and writing any of the variables involves reading the word containing both of them. Concurrent access to the variables might lead to unexpected behavior. That's the issue requiring memory model guarantees. The C++ memory model guarantees that two threads can access and update separate memory locations without interfering with each other.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.